# coding=utf-8
import arcpy
import json

def t1():
    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(r'D:\ArcPy\demoData\tpg1.json'.decode('utf-8'), 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)
    ArcpyPolygon_List = []
    for coordsList in data['geometry']:
        ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
        ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
        ArcpyPolygon = arcpy.Polygon(ArcpyArray_List,spatial_ref)
        ArcpyPolygon_List.append(ArcpyPolygon)
    #第一种方式创建polygon
    arcpy.CopyFeatures_management(ArcpyPolygon_List, r"D:\ArcPy\demoData\1.shp")
    #第二种方式创建polygon
    m_polygon = arcpy.CreateFeatureclass_management(r'/demoData', '2.shp', 'POLYGON')
    arcpy.DefineProjection_management(m_polygon,coor_system='4326')
    cs = arcpy.da.InsertCursor(m_polygon,['SHAPE@'])
    for ArcpyPolygon in ArcpyPolygon_List:
        cs.insertRow([ArcpyPolygon])
    del cs

#arcgis转出的json
def t2():
    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(r'D:\ArcPy\demoData\water.json'.decode('utf-8'), 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)
    dataCount = len(data['features'])
    ArcpyPolygon_List = []
    for i in range(dataCount):
        coordsList_Top = data['features'][i]['geometry']['rings']
        for coordsList in coordsList_Top:
            ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
            ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
            ArcpyPolygon = arcpy.Polygon(ArcpyArray_List,spatial_ref)
            ArcpyPolygon_List.append(ArcpyPolygon)
    arcpy.CopyFeatures_management(ArcpyPolygon_List, r"D:\ArcPy\demoData\1.shp")

#arcgis转出的json带字段
def t3():
    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(r'D:\ArcPy\demoData\water.json'.decode('utf-8'), 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)
    dataCount = len(data['features'])
    ArcpyPolygon_List = []
    for i in range(dataCount):
        coordsList_Top = data['features'][i]['geometry']['rings']
        geoName = data['features'][i]['attributes']['name']
        geoCounty = data['features'][i]['attributes']['county']
        geoType = data['features'][i]['attributes']['type']
        for coordsList in coordsList_Top:
            ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
            ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
            ArcpyPolygon = arcpy.Polygon(ArcpyArray_List,spatial_ref)
            ArcpyPolygon_List.append([ArcpyPolygon,geoName,geoCounty,geoType])
    m_polygon = arcpy.CreateFeatureclass_management(r'/demoData', '2.shp', 'POLYGON')
    arcpy.AddField_management(m_polygon,'name','TEXT')
    arcpy.AddField_management(m_polygon,'county','TEXT')
    arcpy.AddField_management(m_polygon,'type','TEXT')
    arcpy.DefineProjection_management(m_polygon,coor_system='4326')
    cs = arcpy.da.InsertCursor(m_polygon,['SHAPE@','name','county','type'])
    for ArcpyPolygon in ArcpyPolygon_List:
        cs.insertRow(ArcpyPolygon)
    del cs

def t4():
    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(r'D:\ArcPy\demoData\reserve.json'.decode('utf-8'), 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)
    dataCount = len(data['features'])
    ArcpyPolygon_List = []
    for i in range(dataCount):
        coordsList_Top = data['features'][i]['geometry']['rings']
        geoName = data['features'][i]['attributes']['name']
        geoCounty = data['features'][i]['attributes']['county']
        geoType = data['features'][i]['attributes']['type']
        for coordsList in coordsList_Top:
            ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
            ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
            ArcpyPolygon = arcpy.Polygon(ArcpyArray_List,spatial_ref)
            ArcpyPolygon_List.append([ArcpyPolygon,geoName,geoCounty,geoType])
    m_polygon = arcpy.CreateFeatureclass_management(r'/demoData', '2.shp', 'POLYGON')
    arcpy.AddField_management(m_polygon,'name','TEXT')
    arcpy.AddField_management(m_polygon,'county','TEXT')
    arcpy.AddField_management(m_polygon,'type','TEXT')
    arcpy.DefineProjection_management(m_polygon,coor_system='4326')
    cs = arcpy.da.InsertCursor(m_polygon,['SHAPE@','name','county','type'])
    for ArcpyPolygon in ArcpyPolygon_List:
        cs.insertRow(ArcpyPolygon)
    del cs

def t5():
    import arcpy
    import json

    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(r'D:\ArcPy\demoData\river.json'.decode('utf-8'), 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)
    dataCount = len(data['features'])
    ArcpyPolyline_List = []
    for i in range(dataCount):
        coordsList_Top = data['features'][i]['geometry']['paths']
        geoName = data['features'][i]['attributes']['name']
        geoCounty = data['features'][i]['attributes']['county']
        geoType = data['features'][i]['attributes']['level']
        for coordsList in coordsList_Top:
            ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
            ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
            ArcpyPolyline = arcpy.Polyline(ArcpyArray_List, spatial_ref)
            ArcpyPolyline_List.append([ArcpyPolyline, geoName, geoCounty, geoType])
    m_polygon = arcpy.CreateFeatureclass_management(r'/demoData', '2.shp', 'POLYLINE')
    arcpy.AddField_management(m_polygon, 'name', 'TEXT')
    arcpy.AddField_management(m_polygon, 'county', 'TEXT')
    arcpy.AddField_management(m_polygon, 'level', 'TEXT')
    arcpy.DefineProjection_management(m_polygon, coor_system='4326')
    cs = arcpy.da.InsertCursor(m_polygon, ['SHAPE@', 'name', 'county', 'level'])
    for ArcpyPolyline in ArcpyPolyline_List:
        cs.insertRow(ArcpyPolyline)
    del cs

def creatBridge():
    import arcpy
    import json

    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(r'D:\ArcPy\demoData\bridge.json'.decode('utf-8'), 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)
    dataCount = len(data['features'])
    Point_List = []
    for i in range(dataCount):
        rivName = data['features'][i]['river_name'] if data['features'][i]['river_name'] is not None else ""
        bridgeName = data['features'][i]['bridge_name'] if data['features'][i]['bridge_name'] is not None else ""
        bridgeX = data['features'][i]['jd']
        bridgeY = data['features'][i]['wd']
        cn = [bridgeX, bridgeY, rivName, bridgeName]
        Point_List.append(cn)
    m_point = arcpy.CreateFeatureclass_management(r'/demoData', 'createdPoint.shp', 'POINT')
    arcpy.AddField_management(m_point, 'riv_name', 'TEXT')
    arcpy.AddField_management(m_point, 'bri_name', 'TEXT')
    arcpy.DefineProjection_management(m_point, coor_system='4326')
    cs = arcpy.da.InsertCursor(m_point, ['SHAPE@X', 'SHAPE@Y', 'riv_name', 'bri_name'])
    for Point in Point_List:
        print Point
        cs.insertRow(Point)
    del cs
def createGPS():
    import arcpy
    import json

    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(r'D:\ArcPy\demoData\gps.json'.decode('utf-8'), 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)
    dataCount = len(data)
    ArcpyPolygon_List = []
    Point_List = []
    for i in range(dataCount):
        jd = data[i]['lng']
        wd = data[i]['lat']
        cn = [jd, wd]
        Point_List.append(cn)

    ArcpyPoint_List = [arcpy.Point(*coords) for coords in Point_List]
    ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
    ArcpyPolygon = arcpy.Polygon(ArcpyArray_List, spatial_ref)
    ArcpyPolygon_List.append([ArcpyPolygon])
    m_polygon = arcpy.CreateFeatureclass_management(r'/demoData', '2.shp', 'POLYGON')

    arcpy.DefineProjection_management(m_polygon, coor_system='4326')
    cs = arcpy.da.InsertCursor(m_polygon, ['SHAPE@'])
    for ArcpyPolygon in ArcpyPolygon_List:
        cs.insertRow(ArcpyPolygon)
    del cs

def createRoad():
    import arcpy
    import json

    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(r'D:\ArcPy\demoData\road.json'.decode('utf-8'), 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)

    dataCount = len(data['data'])
    ArcpyPolyline_List = []
    for i in range(dataCount):
        roadName = data['data'][i]['roadName']
        print roadName
        coordsList_Top = data['data'][i]['jsonUrl']
        for coordsList in coordsList_Top:
            ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
            ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
            ArcpyPolyline = arcpy.Polyline(ArcpyArray_List, spatial_ref)
            ArcpyPolyline_List.append([ArcpyPolyline, roadName])
    x_polyline = arcpy.CreateFeatureclass_management(r'/demoData', 'road.shp', 'POLYLINE',spatial_reference=spatial_ref)
    arcpy.AddField_management(x_polyline, 'name', 'TEXT')
    arcpy.DefineProjection_management(x_polyline, coor_system='4326')
    cs = arcpy.da.InsertCursor(x_polyline, ['SHAPE@', 'name'])
    for ArcpyPolyline in ArcpyPolyline_List:
        cs.insertRow(ArcpyPolyline)
    del cs